package com.legrand.route.service.impl;

import com.legrand.core.config.RedisUtils;
import com.legrand.core.entity.User;
import com.legrand.core.exception.ExceptionBase;
import com.legrand.core.util.EncryptionUtil;
import com.legrand.route.service.TokenService;
import com.legrand.route.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xiah
 * 2018-06-27 10:52:34
 * 用户权限认证
 */
@Component
public class TokenServiceImpl implements TokenService {
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public String accessTokenCheck(String accessToken, String type) {
        String ss;
        if (type.equals("1")) {
            ss = EncryptionUtil.md5(accessToken);
        } else {
            ss = EncryptionUtil.sha1(accessToken);
        }
        Object object = redisUtils.get(ss);
        if (object != null) {
            User user = (User) object;
            try {
                return JWTUtils.convertUser2JWTToken(user, "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        throw new ExceptionBase(500);
    }

    @Override
    public Boolean otherSecurityCheck(String openId, String type) {
        return true;
    }
}
